package me.keiwu.task.controller.rpc;

import me.keiwu.pub.util.DateUtil;
import me.keiwu.task.domain.Challenge;
import me.keiwu.task.service.ChallengeService;
import me.keiwu.task.util.enums.ChallengeState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


/**
 * Created by kei on 14/11/2017.
 */
@RestController
@RequestMapping("/rpc/challenge")
public class ChallengeRpcController {


    @Autowired
    private ChallengeService challengeService;

    @ResponseBody
    @RequestMapping("/step/update/trigger")
    public void triggerUpdateStep(Long userId) {
        challengeService.triggerUpdateStep(userId);
    }

    @ResponseBody
    @RequestMapping("/list")
    public Page<Challenge> listChallenge(@RequestParam(required = false) Integer state,
                                         @RequestParam(required = false) String afterDate,
                                         @RequestParam(required = false) String beforeDate,
                                         @RequestParam(required = false) String name,
                                         @RequestParam(defaultValue = "0") Integer page,
                                         @RequestParam(defaultValue = "10") Integer size) {
        String pattern = "yyyy-MM-dd";
        Date after = null, before = null;
        if (afterDate != null) after = DateUtil.parseDate(afterDate, pattern);
        if (beforeDate != null) before = DateUtil.getDayEnd(DateUtil.parseDate(beforeDate, pattern));
        Page<Challenge> challenges = challengeService.findAllChallenges(state, after, before, name, page, size);
        Date now = new Date();
        challenges.forEach(c -> {
            Date dateEnd = DateUtil.getDayEnd(c.getEndDate());
            if (null == c.getChallenger() && now.after(dateEnd)) {
                c.setState(ChallengeState.OVERTIME.getState());
            } else if (now.after(dateEnd)) {
                c.setState(ChallengeState.END.getState());
            }
        });
        return challenges;
    }

    @ResponseBody
    @RequestMapping("/list/starting")
    public Page<Challenge> listStartingChallenge(@RequestParam(defaultValue = "0") Integer page,
                                                 @RequestParam(defaultValue = "10") Integer size) {
        return challengeService.findAllStartingChallenges(page, size);
    }


    @ResponseBody
    @RequestMapping("/close")
    public Integer closeChallenge(@RequestParam Long challengeId) {
        Challenge challenge = challengeService.closeChallenge(challengeId);
        return challenge.getState();
    }

    @ResponseBody
    @RequestMapping("/reopen")
    public Integer reopenChallenge(@RequestParam Long challengeId) {
        Challenge challenge = challengeService.reopenChallenge(challengeId);
        return challenge.getState();
    }



    // 单个挑战
    @ResponseBody
    @RequestMapping("/findChallengeById")
    public Challenge findChallengeById(@RequestParam Long challengeId) {
        return challengeService.findChallengeById(challengeId);
    }

    // 任务列表
    @ResponseBody
    @RequestMapping("/list/user")
    public Page<Challenge> findAllUserChallenge(@RequestParam Long userId,
                                                @RequestParam(defaultValue = "0") Integer page,
                                                @RequestParam(defaultValue = "10") Integer size) {
        Page<Challenge> challenges = challengeService.findUserChallenges(userId, page, size);
        Date now = new Date();
        challenges.forEach(c -> {
            if (c.getChallenger() == null && c.getState() == ChallengeState.INIT.getState()
                    && now.after(DateUtil.getDayEnd(c.getEndDate()))) {
                c.setState(ChallengeState.OVERTIME.getState());
            }
        });
        return challenges;
    }

    @ResponseBody
    @RequestMapping("/yesterdayChallenges")
    public Page<Challenge> yesterdayChallenges(@RequestParam(defaultValue = "0") Integer page,
                                                @RequestParam(defaultValue = "100") Integer size) {
        Page<Challenge> challenges = challengeService.findYesterdayChallenge(page, size);

        return challenges;
    }
}
